//
//  WYCRegisterController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCRegisterController.h"
#import "UIButton+CountDown.h"
#import "WYCRealnameController.h"
#import "RSAUtil.h"
#import "QRScanViewController.h"
#import "WYCArgeeViewController.h"
@interface WYCRegisterController ()<QRScanDelegate>

/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;


/** 邀请码*/
@property(nonatomic,strong)UITextField * Rgscodefield;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * ZpasstexfieldButton;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * passtexfieldButton;

@end

@implementation WYCRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //创建界面
    [self setupCreat];
    
}
#pragma make ------->>创建界面
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    
    
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = wycMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 100 * px, 30, 200 * px, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"注册";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, NavRigsterLabel.y, 66 * px , 66 * px);
    [registerView addSubview:backbutton];
    
    
    ////////////////
    //账号输入框/////
    ///////////////
    UILabel * chainalabel = [[UILabel alloc] init];
    chainalabel.frame = CGRectMake(80 * px, CGRectGetMaxY(registerView.frame) + 80 * px,0, 0);
    chainalabel.textColor = [UIColor grayColor];
    [self.view addSubview:chainalabel];
    
    UITextField * usertexfield = [[UITextField alloc] init];
    usertexfield.frame = CGRectMake(CGRectGetMaxX(chainalabel.frame),CGRectGetMaxY(registerView.frame) + 80 * px, ScreenWidth - WYCcellHeight, 140 * px);
    usertexfield.placeholder = @"请输入手机号";
    usertexfield.tintColor = [UIColor redColor];
    usertexfield.backgroundColor = WYCRGBColor(249,249, 249);
    usertexfield.clearButtonMode=UITextFieldViewModeAlways;
    usertexfield.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:usertexfield];
    UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView.backgroundColor = [UIColor clearColor];
    usertexfield.leftView = leftView;
    usertexfield.leftViewMode = UITextFieldViewModeAlways;
    usertexfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    self.UserTextField = usertexfield;
    //手机提示线
    UIView * lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(usertexfield.frame) + 5 * px, usertexfield.width, 1);
    [self.view addSubview:lineView];
    
    UITextField * codefield = [[UITextField alloc] init];
    codefield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(lineView.frame) + 80 * px, ScreenWidth - WYCcellHeight, usertexfield.height);
    codefield.placeholder = @"验证码";
    codefield.tintColor = [UIColor redColor];
    codefield.backgroundColor = WYCRGBColor(249,249, 249);
    codefield.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:codefield];
    _codeTexField = codefield;
    UILabel * leftView1 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView1.backgroundColor = [UIColor clearColor];
    codefield.leftView = leftView1;
    codefield.leftViewMode = UITextFieldViewModeAlways;
    codefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    
    //手机提示线
    UIView * passlineView = [[UIView alloc] init];
    passlineView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(codefield.frame) + 5 * px, lineView.width, 1);
//    passlineView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passlineView];
    
    //获取验证码
    UIButton * codebutton = [[UIButton alloc] init];
    codebutton.frame = CGRectMake(ScreenWidth - 360 * px, codefield.y, 300 * px, codefield.height);
    [codebutton setTitleColor:wycMain forState:UIControlStateNormal];
    codebutton.titleLabel.font = [UIFont systemFontOfSize:SmallFont];
    [codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:codebutton];
    
  
    
    ////////////////
    //密码输入框/////
    ///////////////
    
    
    UITextField * passtexfield = [[UITextField alloc] init];
    passtexfield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(passlineView.frame) + 80 * px, usertexfield.width, usertexfield.height);
    passtexfield.placeholder = @"请输入密码";
    passtexfield.tintColor = [UIColor redColor];
    passtexfield.backgroundColor = WYCRGBColor(249,249, 249);
//    passtexfield.secureTextEntry = YES;
    [self.view addSubview:passtexfield];
    self.passtexfield = passtexfield;
    
    UILabel * leftView2 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView2.backgroundColor = [UIColor clearColor];
    passtexfield.leftView = leftView2;
    passtexfield.leftViewMode = UITextFieldViewModeAlways;
    passtexfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    UIButton * passtexfieldButton = [[UIButton alloc] init];
    passtexfieldButton.frame = CGRectMake(passtexfield.width - 40 * px, passtexfield.y+10, 80 * px, 80 * px);
    [passtexfieldButton addTarget:self action:@selector(passtexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [self.view addSubview:passtexfieldButton];
    self.passtexfieldButton = passtexfieldButton;
    
    
    //手机提示线
    UIView * passView = [[UIView alloc] init];
    passView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(passtexfield.frame) + 5 * px, usertexfield.width, 1);
//    passView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:passView];
    
    
    
    
    
    /************再次输入密码************/
    
    
    UITextField * Zpasstexfield = [[UITextField alloc] init];
    Zpasstexfield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(passView.frame)+80 * px, usertexfield.width, usertexfield.height);
    Zpasstexfield.placeholder = @"再次输入密码";
    Zpasstexfield.tintColor = [UIColor redColor];
    Zpasstexfield.backgroundColor = WYCRGBColor(249,249, 249);
    [self.view addSubview:Zpasstexfield];
    self.Zpasstexfield = Zpasstexfield;
    UILabel * leftView3 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView3.backgroundColor = [UIColor clearColor];
    Zpasstexfield.leftView = leftView3;
    Zpasstexfield.leftViewMode = UITextFieldViewModeAlways;
    Zpasstexfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    
    UIButton * ZpasstexfieldButton = [[UIButton alloc] init];
    ZpasstexfieldButton.frame = CGRectMake(Zpasstexfield.width - 40 * px, Zpasstexfield.y+10, 80 * px, 80 * px);
    [ZpasstexfieldButton addTarget:self action:@selector(ZpasstexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [self.view addSubview:ZpasstexfieldButton];
    self.ZpasstexfieldButton = ZpasstexfieldButton;
    //手机提示线
    UIView * ZpassView = [[UIView alloc] init];
    ZpassView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(Zpasstexfield.frame) + 5 * px, usertexfield.width, 1);
//    ZpassView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:ZpassView];
    
    
    /************再次输入密码************/
    
    
    UITextField * Rgscodefield = [[UITextField alloc] init];
    Rgscodefield.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(ZpassView.frame)+80 * px, usertexfield.width, usertexfield.height);
    Rgscodefield.placeholder = @"请输入注册码";
    Rgscodefield.backgroundColor = WYCRGBColor(249,249, 249);
    Rgscodefield.tintColor = [UIColor redColor];

//    Rgscodefield.secureTextEntry = YES;
    [self.view addSubview:Rgscodefield];
    UILabel * leftView4 = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
    leftView4.backgroundColor = [UIColor clearColor];
    Rgscodefield.leftView = leftView4;
    Rgscodefield.leftViewMode = UITextFieldViewModeAlways;
    Rgscodefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//    self.Zpasstexfield = Rgscodefield;
    self.Rgscodefield = Rgscodefield;
    
    
    UIButton * RgscodefieldButton = [[UIButton alloc] init];
    RgscodefieldButton.frame = CGRectMake(Rgscodefield.width - 40 * px, Rgscodefield.y+10, 80 * px, 80 * px);
    [RgscodefieldButton addTarget:self action:@selector(rgscodeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [RgscodefieldButton setImage:[UIImage imageNamed:@"scanning"] forState:UIControlStateNormal];
    [self.view addSubview:RgscodefieldButton];
    
    //手机提示线
    UIView * RgscodeView = [[UIView alloc] init];
    RgscodeView.frame = CGRectMake(chainalabel.x, CGRectGetMaxY(Rgscodefield.frame) + 5 * px, usertexfield.width, 1);
//    RgscodeView.backgroundColor = WYCGlobalBg;
    [self.view addSubview:RgscodeView];
    
    
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(80 * px, CGRectGetMaxY(RgscodeView.frame) + 200 * px, usertexfield.width, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 3;
    [loginButton setTitle:@"下一步" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:62 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(RegisterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
    //用户须知的按钮
    UIButton * userAgreeBtn = [[UIButton alloc] init];
    userAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:43 * px];
    userAgreeBtn.frame = CGRectMake(loginButton.x + 80,CGRectGetMaxY(loginButton.frame) + 20 * px,20, 20);
    [userAgreeBtn setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateSelected];
    [userAgreeBtn setImage:[UIImage imageNamed:@"nomal"] forState:UIControlStateNormal];
    userAgreeBtn.selected = YES;
    [self.view addSubview:userAgreeBtn];
    UILabel * PromptLable = [[UILabel alloc] init];
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:SmallFont];
    NSString * str = @"我已阅读并同意";
    PromptLable.font = fnt;
    PromptLable.x = userAgreeBtn.x + 30;
    PromptLable.y = CGRectGetMaxY(loginButton.frame) + 30 * px;
    CGSize PromptLableSize = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    PromptLable.text = str;
    PromptLable.textColor = WYCRGBColor(157, 157, 157);
    PromptLable.frame = CGRectMake(PromptLable.x, PromptLable.y, PromptLableSize.width, PromptLableSize.height);
    [self.view addSubview:PromptLable];
    
    
    
    //用户须知的按钮
    UIButton * userAgreement = [[UIButton alloc] init];
    [userAgreement setTitle:@"《用户协议》" forState:UIControlStateNormal];
    userAgreement.titleLabel.font = [UIFont systemFontOfSize:43 * px];
    userAgreement.frame = CGRectMake(CGRectGetMaxX(PromptLable.frame), PromptLable.y, PromptLable.width + 90 * px, PromptLable.height);
    [userAgreement addTarget:self action:@selector(pressAgree) forControlEvents:UIControlEventTouchUpInside];
    userAgreement.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userAgreement setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:userAgreement];
    
    
    UIImageView * bgview = [[UIImageView alloc] init];
    bgview.frame = CGRectMake(0, ScreenHeight-81, ScreenWidth, 81);
    bgview.image = [UIImage imageNamed:@"login_bg"];
    [self.view addSubview:bgview];
}
-(void)pressAgree
{
    NSLog(@"注册协议");
    WYCArgeeViewController  *agree = [[WYCArgeeViewController alloc]init];
    agree.flag = @"333";
    [self presentViewController:agree animated:YES completion:nil];
}
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_UserTextField resignFirstResponder];
    [_codeTexField resignFirstResponder];
    [_passtexfield resignFirstResponder];
    [_Zpasstexfield resignFirstResponder];
}
#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
    WYCLog(@"----------%@",self.UserTextField.text);
    WYCLogFunc;
    if (self.UserTextField.text.length == 0) {
        
        NSString * showStr = @"手机号不能为空";
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:showStr];
        return;
    }else{
        [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,self.UserTextField.text,Ucodekey];
        
        [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
                
            }else{
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }
}

#pragma make ------>>注册按钮
- (void)RegisterClick {
    WYCLogFunc;
    
    // 手机号判断是否为空
    if(self.UserTextField.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号不能为空"];
        return;
    }
    if (![self validateMobile:_UserTextField.text]){
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"手机号输入错误"];
         return;
    }
    //验证码判断是否为空
    if(self.codeTexField.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入验证码"];
        return;
    }
    if(self.passtexfield.text.length == 0)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"密码不能为空"];
        return;
    }
    //密码判断是否为空
    
    if(self.passtexfield.text != self.Zpasstexfield.text)
    {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"两次输入密码不一致"];
        return;
    }
    if (self.Rgscodefield.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"邀请码不能为空"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
    NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    //
    params[@"mobile"] = self.UserTextField.text;
    params[@"password"] = pas;
    params[@"authcode"] = self.codeTexField.text;
    params[@"regCode"] = self.Rgscodefield.text;
    [HttpTool POST:Register params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = [responseObject objectForKey:@"result"];
            WYCRealnameController * authen = [[WYCRealnameController alloc] init];
            [self presentViewController:authen animated:NO completion:nil];
            WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
            
            [WYCAccountTool saveAccount:Account];
            
            [WYCIsAppLoginTool saveIsAppLogin:YES];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
}
- (void)rgscodeButtonClick{
    WYCLogFunc;
    QRScanViewController *vc = [[QRScanViewController alloc] init];
    vc.delegate = self;
    vc.hidesBottomBarWhenPushed = YES;
    [self showViewController:vc sender:nil];
    
}
- (void)passtexfieldButtonClick{
    self.passtexfieldButton.selected = !self.passtexfieldButton.selected;
    if (self.passtexfieldButton.selected == YES) {
        self.passtexfield.secureTextEntry = NO;
    }else{
        
        self.passtexfield.secureTextEntry = YES;
    }
    
}

- (void)ZpasstexfieldButtonClick{
    self.ZpasstexfieldButton.selected = !self.ZpasstexfieldButton.selected;
    if (self.Zpasstexfield.selected == YES) {
        self.Zpasstexfield.secureTextEntry = NO;
    }else{
        
        self.Zpasstexfield.secureTextEntry = YES;
    }
    
}
- (void)qrScanResult:(NSString *)result viewController:(QRScanViewController *)qrScanVC
{
    
    [qrScanVC dismissViewControllerAnimated:NO completion:nil];
    
    self.Rgscodefield.text = result;
}
- (void)usertexfieldButtonClick{
    
    self.UserTextField.text = @"";
}
-(BOOL)validateMobile:(NSString *)mobileNum
{
    NSString *regex = @"^[1]([3-8]{1}[0-9]{1}|59|58|88|89)[0-9]{8}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:mobileNum];
    
    return isMatch;
}
#pragma make ------>>返回按钮
- (void)backClick
{
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

@end
